﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;
using System.Data;
using System.Data.SqlClient;

namespace DataAccess
{
    public class ScheduleDAL : BaseObject
    {
        #region Public Methods
        ///<summary>
        ///Create Obj from ireader.
        ///</summary>

        private Schedule GetObjectFromReader(IDataReader reader)
        {
            Schedule temp = new Schedule();
            temp.ScheduleID = (reader["ScheduleID"] is DBNull) ? int.MinValue : (System.Int32)reader["ScheduleID"];
            temp.TrainID = (reader["TrainID"] is DBNull) ? string.Empty : (System.String)reader["TrainID"];
            temp.StartDate = (reader["StartDate"] is DBNull) ? DateTime.MinValue: (System.DateTime)reader["StartDate"];
            temp.EndDate = (reader["EndDate"] is DBNull) ? DateTime.MinValue : (System.DateTime)reader["EndDate"];
            temp.Status = (reader["Status"] is DBNull) ? 0 : (System.Int32)reader["Status"];
            return temp;
        }

        ///<summary>
        ///Get List Schedule and store in a list 
        ///</summary>
        public Schedule Get_Schedule_By_ID(int ScheduleID)
        {
            using (SqlConnection conn = CreateConnection())
            {
                SqlCommand cmd = new SqlCommand(usp_SelecttbSchedulesByID, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@ScheduleID", ScheduleID));
                conn.Open();
                Schedule customer = new Schedule();
                using (IDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    if (reader.Read())
                    {
                        customer = GetObjectFromReader(reader);
                    }
                }
                conn.Close();
                return customer;
            }
        }

        ///<summary>
        ///Get List Schedule and store in a list
        ///</summary>
        /// <param name="whereSql">Where fields. example : 1=1 </param>
        /// <param name="orderBy"Order fields></param>
        public List<Schedule> GetList_Schedule(string whereSql, string orderBy)
        {
            using (SqlConnection conn = CreateConnection())
            {
                SqlCommand cmd = new SqlCommand(usp_SelecttbSchedulesDynamic, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@WhereCondition", whereSql));
                cmd.Parameters.Add(new SqlParameter("@OrderByExpression", orderBy));
                conn.Open();
                List<Schedule> lst = new List<Schedule>();
                using (IDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    while (reader.Read())
                    {
                        lst.Add(GetObjectFromReader(reader));
                    }
                }
                conn.Close();
                return lst;
            }
        }

        ///<summary>
        ///Get All Schedule and store in a list
        ///</summary>
        /// <param name="whereSql">Where fields. example : 1=1 </param>
        /// <param name="orderBy"Order fields></param>
        public List<Schedule> GetAll_Schedule()
        {
            using (SqlConnection conn = CreateConnection())
            {
                SqlCommand cmd = new SqlCommand(usp_SelecttbSchedulesAll, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                conn.Open();
                List<Schedule> lst = new List<Schedule>();
                using (IDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    while (reader.Read())
                    {
                        lst.Add(GetObjectFromReader(reader));
                    }
                }
                conn.Close();
                return lst;
            }
        }

        ///<summary>
        ///Using for paging data 
        ///</summary>
        /// <param name="pageIndex">Number of pages</param>
        /// <param name="PageSize">Number of a rows per page</param>
        /// <param name="conditions">Where  fields</param>
        /// <param name="groupBy">Group by fields</param>
        public DataTable Get_ScheduleAsPaging(string sWhere, string sOrderBy, int pageIndex, int pageSize)
        {
            using (SqlConnection conn = CreateConnection())
            {
                SqlCommand cmd = new SqlCommand(usp_SelecttbSchedulesPaged, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@Where", sWhere));
                cmd.Parameters.Add(new SqlParameter("@OrderBy", sOrderBy));
                cmd.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
                cmd.Parameters.Add(new SqlParameter("@PageSize", pageSize));
                SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
                try
                {
                    conn.Open();
                    DataTable tbl = new DataTable();
                    dataAdapter.Fill(tbl);
                    conn.Close();
                    return tbl;
                }
                catch
                {
                    return null;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                        conn.Close();
                }
            }
        }

        ///<summary>
        ///Insert new Schedule
        ///</summary>
        public void Insert_Schedule(Schedule temp)
        {
            using (SqlConnection conn = CreateConnection())
            {
                SqlCommand cmd = new SqlCommand(usp_InserttbSchedule, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@ScheduleID", temp.ScheduleID));
                cmd.Parameters.Add(new SqlParameter("@TrainID", temp.TrainID));
                cmd.Parameters.Add(new SqlParameter("@StartDate", temp.StartDate));
                cmd.Parameters.Add(new SqlParameter("@EndDate", temp.EndDate));
                cmd.Parameters.Add(new SqlParameter("@Status", temp.Status));

                try
                {
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
                catch (SqlException ex)
                {
                    throw ex;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                        conn.Close();
                }
            }
        }

        ///<summary>
        ///Update a Schedule
        ///</summary>
        public void Update_Schedule(Schedule temp)
        {
            using (SqlConnection conn = CreateConnection())
            {
                SqlCommand cmd = new SqlCommand(usp_UpdatetbSchedule, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@ScheduleID", temp.ScheduleID));
                cmd.Parameters.Add(new SqlParameter("@TrainID", temp.TrainID));
                cmd.Parameters.Add(new SqlParameter("@StartDate", temp.StartDate));
                cmd.Parameters.Add(new SqlParameter("@EndDate", temp.EndDate));
                cmd.Parameters.Add(new SqlParameter("@Status", temp.Status));
                try
                {
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
                catch (SqlException ex)
                {
                    throw ex;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                        conn.Close();
                }
            }
        }

        ///<summary>
        ///Delete a Schedule
        ///</summary>
        public void Delete_Schedule(Schedule temp)
        {
            using (SqlConnection conn = CreateConnection())
            {
                SqlCommand cmd = new SqlCommand(usp_DeletetbSchedule, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@ScheduleID", temp.ScheduleID));
                try
                {
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
                catch (SqlException ex)
                {
                    throw ex;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                        conn.Close();
                }
            }
        }

        #endregion
    
    }
}
